/*正则表达式*/
var defreg = {
    "img": /(\.|\/)(gif|jpe?g|png)$/i,
    "doc": /(\.|\/)(pdf|doc|docx|ppt|pptx)$/i,
    "mobile": /^0?(13|15|18|14|17)[0-9]{9}$/,
    "email": /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,4}$/,
    "emailmobile": /^1\d{10}$|^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,4}$/,
    "password": /^[^\u4E00-\u9FA5\uF900-\uFA2D\u0020]{6,16}$/,
    "loginname": /^[^\u4E00-\u9FA5\uF900-\uFA2D\u0020]{26}$/,
    //"password":/^[0-9a-zA-Z]{8,16}$/,
    "url": /((https|http|ftp|rtsp|mms):\/\/)?(([0-9a-z_!~*'().&=+$%-]+:)?[0-9a-z_!~*'().&=+$%-]+@)?(([0-9]{1,3}\.){3}[0-9]{1,3}|([0-9a-z_!~*'()-]+\.)*([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\.[a-z]{2,6})(:[0-9]{1,4})?((\/?)|(\/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+\/?)/g,
    "mobilecode": /^\d{6}$/,
    "yaocode": /^\d{6}$/,
    "realname": /^[\u4e00-\u9fa5]{2,5}$/i,
    "amount": /^[0-9]+([.]{1}[0-9]{1,2})?$/,
    "vcode": /^\w{4}$/
};
/*弹出窗口插件*/
function OpenDiv() {
    this.cfg = {
        id: 'open' + new Date().getTime(),
        content: '',
        close: false,
        mask: null,
        dom: $(jQuery('<div class="opendiv"></div>')),
        z_Index: 10000,
    };
};
OpenDiv.prototype = {
    addWrap: function() {
        if (!$('.open_mask').length) {
            this.cfg.mask = $(jQuery("<div class='open_mask'></div>"));
        } else {
            this.cfg.mask = $('.open_mask').eq(0);
            this.cfg.mask.css('display', 'block');
        }
        this.cfg.mask.appendTo('body');
    },
    show: function(options, callback) {
        $.extend(this.cfg, options);
        this.cfg.dom.html(this.cfg.content);
        if (this.cfg.close) {
            oclose = $("<span class='topclose closeopen'><span class='no1'><img src='//jrstatic.kongzhong.com/jrweb/img/new_close1.png' alt='' /></span><span class='no2'><img src='//jrstatic.kongzhong.com/jrweb/img/new_close2.png' alt='' /></span></span>");
            oclose.appendTo(this.cfg.dom);
        }
        this.cfg.dom.appendTo('body');
        var zindex = this.cfg.z_Index + ($('.opendiv').length - 1) * 2; // mask灞傜骇
        this.addWrap();
        this.cfg.mask.css({
            'z-index': zindex,
        });
        this.cfg.dom.attr('id', this.cfg.id).css({
            'z-index': zindex + 1,
        });
        this.cfg.mask.add(this.cfg.dom);
        othis = this;
        var thisObj = this.cfg.dom;
        thisObj.css({
            'left': ($(window).width() - thisObj.outerWidth()) / 2, // window.innerWidth
            'top': ($(window).height() - thisObj.outerHeight()) / 2, // window.innerHeight
        });
        $(window).resize(function() {
            othis.resizefn(thisObj);
        });

        thisObj.find('.closeopen').click(function() {
            othis.closediv(thisObj);
        });
        thisObj.find('.topclose').click(function() {
            othis.closediv(thisObj);
            //          window.location.reload();
        });
        (callback || function() {})(thisObj);
    },
    closediv: function(obj) {
        var othis = this;
        obj.remove();
        var zindex = othis.cfg.z_Index + ($('.opendiv').length - 1) * 2;
        if ($('.opendiv').length <= 0) {
            othis.cfg.mask.css('display', 'none');
        } else {
            othis.cfg.mask.css({
                'z-index': zindex,
            });
        }
    },
    resizefn: function(obj) {
        /* obj.animate({
            left: (window.innerWidth - obj.outerWidth()) / 2,
            top: (window.innerHeight - obj.outerHeight()) / 2
        }, 200, function() {
        }); */
        obj.css({
            'left': (window.innerWidth - obj.outerWidth()) / 2,
            'top': (window.innerHeight - obj.outerHeight()) / 2,
        });
    },

};

function alert_msg(msg) {
    var temp_html = "<div class='openalert'><div class='opentitle' style='text-align:center'>温馨提示</div><div class='opencon'>" + msg + "</div><div class='openbot'><input type='button' name='alert_btn' value='确定' class='org_btn closeopen'></div></div>"
    var newalert = new OpenDiv();
    newalert.show({
        "content": temp_html
    }, function(eobj) {});
    return newalert
};
//输入框交互提示
$.fn.inputtips = function(params) {
    var $this = $(this);
    if ($this.length == 0) {
        return;
    };
    var params = $.extend({
        left: 0,
        top: 40,
        reg: null,
        callback: null
    }, params);
    var $this = $(this);
    var $dis = null;
    var focus_text = $this.data("focus");
    var error_text = $this.data("error");
    var error_empty = $this.data("empty");
    var init = function() {
        if (!$this.siblings(".inputtips").length) {
            $dis = $("<div class='inputtips'></div>");
            $dis.insertAfter($this);
            $dis.css({
                'left': params.left + 'px',
                'top': params.top + 'px'
            })
        } else {
            $dis = $this.siblings(".infodis").empty();
        }
    }
    var bclear = function() {
        $this.removeClass("error").removeClass("focus");
        $dis.removeClass("error").removeClass("focus").empty();
    }
    $this.on('focus', function() {
        bclear();
        $this.addClass("focus");
        $dis.addClass("focus");
        $dis.html(focus_text);
    });
    $this.on('blur', function() {
        var istr = $this.val();
        bclear();
        if (error_empty == "" && istr == "") { //元素可为空且输入为空
            bclear();
            $this.data("verify", "true");
            return
        }
        if (error_empty != "" && istr == "") { //元素不能为空而输入为空
            bclear();
            $this.data("verify", "flase");
            $dis.addClass("error");
            $dis.html(error_empty);
            return
        }
        if (params.reg && !defcheck(istr, params.reg)) {
            bclear();
            $this.data("verify", "flase");
            $dis.addClass("error");
            $dis.html(error_text);
            return
        }
        if (params.reg && defcheck(istr, params.reg) && params.callback) {
            bclear();
            d = params.callback($this.val());
            if (d.statu) {
                $this.data("verify", "true");
            } else {
                $dis.html(d.msg);
                $dis.addClass("error");
            }
            return
        }
        if (!params.reg && !params.callback) {
            bclear();
            $this.data("verify", "true");
            return
        }
        if (!params.reg && params.callback) {
            bclear();
            d = params.callback($this.val());
            if (d.statu) {
                $this.data("verify", "true");
            } else {
                $dis.html(d.msg);
                $dis.addClass("error");
            }
            return
        }
        $this.data("verify", "true");
    });
    $this.bind('view.disinfo', function(event, info) {
        bclear();
        $dis.addClass("error");
        $dis.html(info)
    });
    init();
};

function checkidcard(str) {
    var temp = {};
    if (id_cards.checkCard(str)) {
        temp.msg = "";
        temp.statu = true;
        return temp;
    } else {
        temp.msg = "身份证号格式不正确";
        temp.statu = false;
        return temp;
    }
}

//身份证号码
var id_cards = {
    cities: {
        11: "北京",
        12: "天津",
        13: "河北",
        14: "山西",
        15: "内蒙古",
        21: "辽宁",
        22: "吉林",
        23: "黑龙江",
        31: "上海",
        32: "江苏",
        33: "浙江",
        34: "安徽",
        35: "福建",
        36: "江西",
        37: "山东",
        41: "河南",
        42: "湖北",
        43: "湖南",
        44: "广东",
        45: "广西",
        46: "海南",
        50: "重庆",
        51: "四川",
        52: "贵州",
        53: "云南",
        54: "西藏",
        61: "陕西",
        62: "甘肃",
        63: "青海",
        64: "宁夏",
        65: "新疆",
        71: "台湾",
        81: "香港",
        82: "澳门",
        91: "国外"
    },
    checkCard: function(a) {
        return this.isCardNo(a) && this.checkProvince(a) && this.checkBirthday(a) && this.checkParity(a)
    },
    isCardNo: function(a) {
        var b = /(^\d{15}$)|(^\d{17}(\d|X)$)/i;
        return b.test(a)
    },
    checkProvince: function(a) {
        var b = a.substr(0, 2);
        return void 0 == this.cities[b] ? !1 : !0
    },
    checkBirthday: function(a) {
        var b = a.length;
        if ("15" == b) {
            var c = /^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/,
                d = a.match(c),
                e = d[2],
                f = d[3],
                g = d[4],
                h = new Date("19" + e + "/" + f + "/" + g);
            return this.verifyBirthday("19" + e, f, g, h)
        }
        if ("18" == b) {
            var i = /^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/i,
                d = a.match(i),
                e = d[2],
                f = d[3],
                g = d[4],
                h = new Date(e + "/" + f + "/" + g);
            return this.verifyBirthday(e, f, g, h)
        }
        return !1
    },
    checkParity: function(a) {
        var b = a.length;
        if ("18" == b) {
            var c, d, e = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2),
                f = new Array("1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"),
                g = 0;
            for (c = 0; 17 > c; c++) g += a.substr(c, 1) * e[c];
            return d = f[g % 11],
                d == a.substr(17, 1).toUpperCase() ? !0 : !1
        }
        return !0
    },
    verifyBirthday: function(a, b, c, d) {
        var e = new Date,
            f = e.getFullYear();
        if (d.getFullYear() == a && d.getMonth() + 1 == b && d.getDate() == c) {
            var g = f - a;
            return g >= 0 && 120 >= g ? !0 : !1
        }
        return !1
    }
};

var realname = function() {
    var temp_html = "<div class='realnwarp bosshow'><div class='realntitle'>实名认证</div><div class='realncom'><div class='realitem'><div class='realtit'>真实姓名</div><div class='relinright'><input autocomplete='off' class='realcinput' type='text' id='trun_name' name='trun_name' data-focus = '请输入真实姓名' data-error = '真实姓名格式错误' data-empty = '真实姓名不能为空' placeholder='请输入真实姓名' maxlength='10'></div></div><div class='realitem'><div class='realtit'>身份证号</div><div class='relinright'><input autocomplete='off' class='realcinput' type='text' id='trun_card' name='trun_card' data-focus = '请输入身份证号码' data-error = '身份证号码格式错误' data-empty = '身份证号码不能为空' placeholder='请输入身份证号码' maxlength='18'></div></div><div class='realmiaoshu'></div><div class='realbota'><input type='button' class='org_btn' id='relnamebar' value='提交信息'><input type='button' class='cancel_btn closeopen' value='取　消'></div></div></div>"
    var newalert = new OpenDiv();
    newalert.show({
        "content": temp_html,
        "close": true
    }, function(eobj) {
        $("#trun_name").inputtips({
            left: 0,
            top: 40,
            reg: "realname",
            callback: null
        });
        $("#trun_card").inputtips({
            left: 0,
            top: 42,
            reg: null,
            callback: null
        });
        $("#relnamebar").click(function(e) {
            $('#trun_name').add("#trun_card").trigger('blur');
            if (($('#trun_name').data("verify") == "true") && ($('#trun_card').data("verify") == "true")) {
                sdata = {};
                sdata.name = $.trim($("#trun_name").val());
                sdata.idcard = $.trim($("#trun_card").val());
                var realdata = { "id": "1", "jsonrpc": "2.0", "method": "addTouristIdentity", "params": { "tourist": { "name": $("#trun_name").val(), "identity": $("#trun_card").val() } } }


            }
        })
    });
};